//
//  ContactViewController.m
//  AoJi
//
//  Created by Administrator on 15/1/20.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import "ContactViewController.h"

@interface ContactViewController ()
{
    
}

@end

@implementation ContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sidemenuopened) name:kSideMenuOpened object:nil];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    //上面背景图片
    UIImageView *headView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg1.png"]];
    UIImageView *whiteImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bj3"]];
    whiteImg.frame=CGRectMake(0, 240, self.view.frame.size.width, 83);
    [headView addSubview:whiteImg];


    //导航栏
    _titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    _titleView.backgroundColor=[UIColor clearColor];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 25, 100, 30)];
    titleLabel.text=@"联系澳际";
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    [_titleView addSubview:titleLabel];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame=CGRectMake(10, 20, 44, 44);
    [back setImage:[UIImage imageNamed:@"fanhuibai"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:back];
    [self.view addSubview:_titleView];


    //tableview
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.tableHeaderView=headView;
    _tableView.backgroundColor=[UIColor whiteColor];
    _tableView.bounces=NO;
    //清除cell分割线
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    
//    if (kSideMenuOpen) {
//      [_tableView scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:YES];
//    }
    

    [self loadData];
}

//加载数据  接口company  http://cent.dsgrd.com/company
-(void)loadData
{
    [YHNetwork getInformationWithReguestType:RequestTypeGet WithParameters:nil WithApi:@"company" WhenSuccess:^(NSMutableDictionary *response) {
        _dataSource = (NSArray*)response;
        [_tableView reloadData]; //加载数据以后加载视图
        [self.view addSubview:_tableView];
        //在tabelview之后加载
        [self.view bringSubviewToFront:_titleView];
    } WhenFailure:^(NSError *failure) {
        NSLog(@"%@",failure.localizedDescription);
    }];
}


//返回侧边栏
-(void)back
{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:kSideMenuOpen object:@"3"];
    [_drawer open];
    
}


#pragma mark tableview
//有几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}

//一个分区有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *subCompanies = _dataSource[section][@"subCompanies"];
    return subCompanies.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indetifier =@"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
    }
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, 85,tableView.frame.size.width,1)];
    line.backgroundColor = [UIColor colorWithRed: 220.0f/255.0f green:220.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
    [cell addSubview:line];
    
    cell.userInteractionEnabled=NO;
    cell.backgroundColor=[UIColor clearColor];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSArray *subCompanies = _dataSource[indexPath.section][@"subCompanies"];
    NSDictionary *dict = subCompanies[indexPath.row];
    
    UILabel *name = [self createLabelWithPoint:CGPointMake(12, 25) title:dict[@"name"] fontSize:14 textColor:[UIColor grayColor]];
    [cell.contentView addSubview:name];
    
    UILabel *iphone = [self createLabelWithPoint:CGPointMake(89, 25) title:dict[@"telphone"] fontSize:14 textColor:[UIColor grayColor]];
    [cell.contentView addSubview:iphone];
    
    UILabel *address = [self createLabelWithPoint:CGPointMake(12, 55) title:dict[@"address"] fontSize:14 textColor:[UIColor blackColor]];
    [cell.contentView addSubview:address];
    
    return cell;
}

//系统的不能改变颜色,字体大小,自定义section head分区
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 35)];
    headerView.backgroundColor=[UIColor whiteColor];
    
    UILabel * addressSection= [self createLabelWithPoint:CGPointMake(12, 10) title:_dataSource[section][@"address"] fontSize:18 textColor:nil];
    [headerView addSubview:addressSection];
    
    return headerView;
}

//section head的高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}

//定义label
-(UILabel *)createLabelWithPoint:(CGPoint)point title:(NSString *)title fontSize:(CGFloat)size textColor:(UIColor *)color
{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(point.x, point.y, 0, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:size];
    label.backgroundColor = [UIColor clearColor];
    label.textColor =color;
    [label sizeToFit]; //label大小根据字体大小
    return label;
}

#pragma mark - ICSDrawerController

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerWillClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)sidemenuopened
{
    
//    NSIndexPath *pathe=[NSIndexPath indexPathForRow:0 inSection:0];
    _tableView.contentOffset=CGPointMake(0, 0);
//    [_tableView  scrollToRowAtIndexPath:pathe atScrollPosition:UITableViewScrollPositionTop animated:NO];


}

@end
